package demo.actions;

import demo.services.IModifyService;
import demo.services.IQueryService;
import framework.annotation.InchAutowired;
import framework.annotation.InchController;
import framework.annotation.InchRequestMapping;
import framework.annotation.InchRequestParam;
import framework.webmvc.component.InchModelAndView;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * main controller for this project including 2 transaction: QueryService,ModifyService
 */
@InchController
@InchRequestMapping("/sql")
public class SqlAction {
    //Store transactions
    @InchAutowired IQueryService queryService;
    @InchAutowired IModifyService modifyService;

    @InchRequestMapping("/query")
    public InchModelAndView query(@InchRequestParam("name") String name) {
        //transaction service used
        String result = queryService.query(name);
        //create model
        Map<String,Object> model = new ConcurrentHashMap<>();
        model.put("method","QueryService.query");
        model.put("result", result);
        //create model and view to show
        return new InchModelAndView("sqlAction.html",model);
    }

    @InchRequestMapping("/add")
    public InchModelAndView add(@InchRequestParam("name") String name, @InchRequestParam("address") String address) {
        String result = modifyService.add(name,address);
        Map<String,Object> model = new ConcurrentHashMap<>();
        model.put("method","ModifyService.add");
        model.put("result", result);
        return new InchModelAndView("sqlAction.html",model);
    }

    @InchRequestMapping("/update")
    public InchModelAndView update(@InchRequestParam("name") String name, @InchRequestParam("address") String address) {
        String result = modifyService.update(name,address);
        Map<String,Object> model = new ConcurrentHashMap<>();
        model.put("method","ModifyService.update");
        model.put("result", result);
        return new InchModelAndView("sqlAction.html",model);
    }

    @InchRequestMapping("/remove")
    public InchModelAndView update(@InchRequestParam("id") String id) throws Exception {
        String result = modifyService.remove(id);
        Map<String,Object> model = new ConcurrentHashMap<>();
        model.put("method","ModifyService.remove");
        model.put("result", result);
        return new InchModelAndView("sqlAction.html",model);
    }


}
